package adventure.commands;

import adventure.*;
import adventure.misc.Currency;
import player.*;
import adventure.placeable.Item;
import colour.ColourUtil;

/**
 *
 * @author Michael Hanns
 *
 */
public class Loot extends AbstractCommand {

    Currency currency;

    public Loot(Adventure a) {
        super(a, new String[]{"LOOT"});
        this.currency = a.getCurrency();
    }

    @Override
    public String validateAndExecute(Player player, String[] args) {
        if (args.length == 1) {
            return "Loot what?";
        } else {
            if (((AdventureEdit) adv).getRoomManager().isLightPresent(
                    player.x(), player.y(), player.z())) {
                return loot(player, Util.consolidateArguments(args));
            } else {
                return "You cannot see anything in the darkness!";
            }
        }
    }

    private String loot(Player player, String container) {
        int x = player.x();
        int y = player.y();
        int z = player.z();

        if (rooms.getRoomMobile(x, y, z, container).getID() > 0) {
            String mobile = rooms.getRoomMobileName(x, y, z, container);

            Object[] items = rooms.lootRoomContainer(x, y, z, container);

            if (items.length > 1) {
                // Get the currency val first
                int currencyTaken = Integer.parseInt(String.valueOf(items[0]));

                String output = "";
                output += getCurrency(player, currencyTaken, mobile);

                for (int c = 1; c < items.length; c++) {
                    player.giveItem((Item) items[c]);
                    output += "\nYou get "
                            + ColourUtil.colourise(((Item) items[c]).getName(), ColourUtil.cyan)
                            + " from "
                            + ColourUtil.colourise(mobile, ColourUtil.red) + ".";
                    out.broadcastLocalToPlayer(player,
                            ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                            + " gets "
                            + ColourUtil.colourise(((Item) items[c]).getName(), ColourUtil.cyan)
                            + " from "
                            + ColourUtil.colourise(mobile, ColourUtil.red) + ".");
                }

                return output;
            } else {
                // Only one value indicates possibility of gold only.
                int currencyTaken = Integer.parseInt(String.valueOf(items[0]));

                String currencyOut = getCurrency(player, currencyTaken, mobile);

                if (currencyOut.length() > 0) {
                    return currencyOut;
                } else {
                    return "You cannot loot any " + container + "!";
                }
            }
        } else if (rooms.getRoomPlayer(x, y, z, container) > 0) {
            if (players.getOnlinePlayer(rooms.getRoomPlayer(x, y, z, container)).isDead()) {
                out.broadcastToPlayer(players.getOnlinePlayer(rooms.getRoomPlayer(x, y, z, container)),
                        ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                        + " thought about looting you, but thought better of it.");
                return "As you approach the corpse to loot, you notice a glimmer of life.\n"
                        + "You decide against looting this corpse.";
            } else {
                return ColourUtil.colourise(
                        players.getOnlinePlayer(container).getAliveName(), ColourUtil.magenta)
                        + " is still alive!";
            }
        } else {
            return "You cannot loot any " + container + "!";
        }
    }

    private String getCurrency(Player player, int amount, String mobile) {
        String output = "";
        if (amount > 0) {
            String currencyName;
            if (amount > 1) {
                currencyName = amount + " " + currency.getPlural();
            } else {
                currencyName = "a " + currency.getSingular();
            }

            output += "\nYou get "
                    + ColourUtil.colourise(currencyName, ColourUtil.yellow)
                    + " from " + ColourUtil.colourise(mobile, ColourUtil.red)
                    + ".";
            out.broadcastLocalToPlayer(player,
                    ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                    + " gets " + ColourUtil.colourise(currencyName, ColourUtil.yellow)
                    + " from "
                    + ColourUtil.colourise(mobile, ColourUtil.red)
                    + ".");
        }
        return output;
    }
}
